<template>
	<view class="login">
		<view class="txt">登录帮你吖</view>
		<image src="@/static/cart/icon.png" class="img"></image>
		<view class="btn" @tap="login">授权登录</view>
		<view class="fixed-block">
			<checkbox-group @change="checkBoxChange">
				<label>
					<checkbox value="isCheck" style="transform:scale(0.7)" :checked="isCheck" />
				</label>
			</checkbox-group>
			我以阅读并同意
			<span @tap='userOne'>用户协议</span>
			和
			<span @tap='userTwo'>隐私协议</span>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isCheck: true,
			}
		},
		methods: {
			checkBoxChange(e) {
				console.log('e', e)
				let arr = e.detail.value

				this.isCheck = arr.length > 0 ? true : false
			},
			login() {
				let code;
				let self = this
				if (this.isCheck == false) {
					return self.$tools.showMsg('请先勾选同意授权协议')
				}
				uni.showLoading({
					mask: true,
					title: '登录中...'
				})

				uni.login({
					provider: 'weixin',
					success(login) {
						code = login.code
					},
					fail: (err) => {
						uni.hideLoading()
						self.$tools.showMsg('取消登录')
					}
				})
				// desc: '用于完善会员资料'  必填 声明获取用户个人信息后的用途，后续会展示在弹窗中
				uni.getUserProfile({
					desc: '用于完善用户资料',
					lang: 'zh_CN',
					success(user) {
					 

						//微信登录  
						const data = {
							code: code,
							nickName: user.userInfo.nickName,
							avatar: user.userInfo.avatarUrl,
						}

						self.wxLogin(data)
					},
					fail(err) {
						uni.hideLoading()
						self.$tools.showMsg('取消登录')
					}
				})
			},
			//授权登录
			wxLogin(data) {
				let superior_id = uni.getStorageSync('superior_id') || ''
				this.$http('common.wxLogin', {
					...data,
					superior_id: superior_id,
					superior_type: 0,
				}).then(res => {

					let result = res
					uni.setStorageSync('token', result.data.token)
					// this.getUserInfo()
					this.$tools.showMsg('登录成功')
					uni.hideLoading()
					uni.navigateBack()
				})
			},
			getUserInfo() {
				this.$http('common.getUserInfo', {
					token: this.$tools.getToken()
				}).then(res => {
					if (res.code == 200) {
						this.userInfo = res.data.userData
					}
				})
			},
			userOne() {
				uni.navigateTo({
					url: '/pages/tabbar/user/agreement/agreement'
				})
			},
			userTwo() {
				uni.navigateTo({
					url: '/pages/tabbar/user/privacyPolicy/privacyPolicy'
				})
			}
		}
	}

</script>

<style lang="scss">
	.login {

		height: 100vh;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.txt {
		margin-top: 60rpx;
		font-size: 38rpx;
		font-weight: 500;
	}

	.img {
		margin-top: 60rpx;
		width: 94rpx;
		height: 94rpx;
	}

	.btn {
		margin-top: 60rpx;
		background: #FECD33;
		width: 650rpx;
		line-height: 90rpx;
		text-align: center;
	}

	.fixed-block {
		position: fixed;
		bottom: 120rpx;
		left: 0;
		right: 0;
		display: flex;
		align-items: center;
		justify-content: center;

		span {
			margin: 0 14rpx;
			color: #4998DF;
		}
	}

</style>

